package com.fps.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fps.web.service.BaseService;
import com.fpswork.Helper.DateEditor;
import com.fpswork.core.model.ExcelEntity;
import com.fpswork.core.model.ExcelShell;
import com.fpswork.util.ExcelUtil;


@Controller
public class BaseController {
	  
	    @Autowired
	    BaseService baseService;
    
	 	@InitBinder
	    protected void initBinder(WebDataBinder binder){
	        //binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true));
	        binder.registerCustomEditor(Date.class, new DateEditor());
	        binder.registerCustomEditor(double.class, new CustomNumberEditor(Double.class,true));
	        binder.registerCustomEditor(long.class, new CustomNumberEditor(Long.class,true));
	        binder.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class,true));
	        binder.registerCustomEditor(float.class, new CustomNumberEditor(Float.class,true));
	        binder.registerCustomEditor(boolean.class, new CustomBooleanEditor(true));
	        binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class,true));
	        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class,true));
	        binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class,true));
	        binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class,true));
	        binder.registerCustomEditor(Boolean.class, new  CustomBooleanEditor(true));
	    }
	 	/**
	   	 * 跳转界面
	   	 * SpringMVC中是不可以通过@PathVariable这个注解来传递对象的，原因其实很简单：
	   	 * @PathVariable 是用来获得请求url中的动态参数的，所以该注解只能支持将参数放在请求url的GET提交方式，
	   	 * 所以不管你如何进行设置，@PathVariable都是无法支持Post请求的。
	   	 */
	    @RequestMapping("/{requestName}")
	    public ModelAndView translate(@PathVariable String requestName,String responsePath) throws Exception{
	       	ModelAndView modelAndView = new ModelAndView();   
	       	modelAndView.setViewName(responsePath);   
			return modelAndView;
	    }
	    
	    
	 	/**
	   	 * DownExcel
	   	 * 下载excel用例
	   	 */
	    @RequestMapping(value = "/DownExecl", method = RequestMethod.GET)
	    public @ResponseBody Map<String, String> DownExecl(HttpServletResponse res,HttpServletRequest req) throws Exception{
	       	Map map = new HashMap();
	       	ExcelEntity excelEntity = new ExcelEntity();
	       	List<ExcelShell> filedata = new ArrayList<ExcelShell>();
	       	ExcelShell excelShell = new ExcelShell();
	       	//下载文件名
	    	String fileName="用户数据.xls";
	    	excelEntity.setFilename(fileName);
	    	//存放对应的表头名称和对应的数据库字段
	    	Map<String, Object> titlemap = new HashMap<String, Object>();
	    	titlemap.put("id", "ID");
	    	titlemap.put("username", "用户名");
	    	titlemap.put("realname", "管理员");
	    	titlemap.put("telephone", "电话");
	    	titlemap.put("password", "密码");
	    	excelEntity.setLinename(titlemap);
	    	//直接查询结果集的语句
	    	String sql="select * from baseuser";
	    	excelEntity.setSql(sql);
	    	baseService.DownExecl(excelEntity);
	       	ExcelUtil.DownExecl(res, req,excelEntity);
	    	map.put("flag", "true");
	        return map;
	    }
	    public void DownExecl(HttpServletResponse res,HttpServletRequest req,ExcelEntity excelEntity) throws Exception{
	    	baseService.DownExecl(excelEntity);
	       	ExcelUtil.DownExecl(res, req,excelEntity);
	    }
		

}